package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.entity.EduCourse;
import com.zsr.example.service.EduCourseService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.constants.CourseConstants;
import com.zsr.example.vo.CourseInfoVo;
import com.zsr.example.vo.CoursePublishVo;
import com.zsr.example.vo.CourseReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zsr.example.util.constants.SysLogConstants.DELETE;
import static com.zsr.example.util.constants.SysLogConstants.UPDATE;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-11-15
 */
@RestController
@RequestMapping("/course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @PostMapping("addCourseInfo")
    public JSONObject addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String courseId = courseService.saveCourseInfo(courseInfoVo);

        return CommonUtil.successJson(courseId);

    }

    /**
     * 获取课程详情
     * @param courseId
     * @return
     */
    @GetMapping("getCourseInfo/{courseId}")
    public JSONObject getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo info = courseService.getCourseInfoById(courseId);

        return CommonUtil.successJson(info);
    }

    /**
     * 更新课程
     * @param courseInfoVo
     * @return
     */
    @OptLog(optType = UPDATE,optDesc = "更新录播课")
    @PostMapping("updateCourseInfo")
    @CacheEvict(value = "course",allEntries=true,cacheManager="redisCacheManager")//清空
    public JSONObject updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfoById(courseInfoVo);
        return CommonUtil.success();
    }

    @GetMapping("getCoursePublishVoById/{id}")
    public JSONObject getCoursePublishVoById(
            @PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        return CommonUtil.successJson(coursePublishVo);
    }

    @PutMapping("publishCourse/{id}")
    public JSONObject publishCourseById(
            @PathVariable String id) {

        boolean result = courseService.publishCourseById(id);
        return result ? CommonUtil.success() : CommonUtil.errorJson();
    }

    /**
     * 带条件分页查询课程
     * @param page 页码
     * @param limit 条数
     * @param courseReq 条件
     * @return
     */
    @PostMapping("pageListCourseCondition/{page}/{limit}")
    public JSONObject pageQuery(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody(required = false) CourseReq courseReq) {

        Page<EduCourse> pageParam = new Page<>(page, limit);

        courseService.pageQuery(pageParam, courseReq);

        //TODO 显示课程下现有的章节和小节
        List<EduCourse> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return CommonUtil.successPage(total, records);
    }

    @OptLog(optType = DELETE,optDesc = "删除录播课")
    @DeleteMapping("{id}")
    public JSONObject removeById(
            @PathVariable String id) {

        boolean result = courseService.removeCourseById(id);

        return result ? CommonUtil.success() : CommonUtil.errorJson();
    }


    /**
     * 获取所有课程id和名称 
     * @return
     */
    @GetMapping("getAllCourse")
    public JSONObject getAllCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.select("id","title").eq("status", CourseConstants.STATUS_NORMAL);
        List<EduCourse> list = courseService.list(wrapper);
        return CommonUtil.successJson(list);
    }


}

